define(function(require, exports){
	var $ = require("jquery");
	var pubjs = require('pubjs');
	var view = require('@base/view');
	var util = require('util');
	var Edit = require('@mod/fieldEdit').base;
	var dspUtil = require('dspUtil');

	exports.base = Edit.extend({
		init:function(config){
			config = pubjs.conf(config, {
				fields: [
					{text: LANG('列表：'), type: 'custom', field: 'grid',
						uri: 'blackMagic/action/run.list',
						config: { }
					}
					,{text: LANG('LoginId'), tip: LANG('登录的Id筛选'), type: 'input', field: 'Lid'}
					,{text: LANG('UserId'), tip: LANG('登录的用户Id筛选'), type: 'input', field: 'Uid'}
					,{text: LANG('PrimaryId'), tip: LANG('主账号Id筛选'), type: 'input', field: 'Pid'}
					,{text: LANG('分页'), tip: LANG('默认 1'), type: 'input', field: 'page'}
					,{text: LANG('分页大小'), tip: LANG('每页大小，不要大于20，不好展示哦, 默认 10'), type: 'input', field: 'size'}
					,{text: LANG('返回字段'), tip: LANG('JSON 数组形式, 如["UserId"]'), type: 'input', field: 'fields', style:"width: 500px"}
					,{text: LANG(''), type: 'button', field: 'session', config: {
						'value': LANG('Session查询'),
						'width': 300,
						'class': 'btn btn-primary'
					}}
				],
				isPop: true,
				Title: LANG('运行时状态')
			});

			this.$id = null;
			this.Super('init', arguments);
		}
		,dataChange: function(field, v) {
			this.Super('dataChange', arguments);
			if(field === 'session')
			{
				var data = this.getData();
				if(this.validate(data))
				{
					delete data._id;
					delete data.session;
					delete data.grid;
					var url = 'api/admin/blackMagic?type=session' + dspUtil.queryString(data);
					window.open(url);
				}
			}
		}
	});


	var List = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				urlRemove: 'blackMagic/removeSession'
			});
			this.Super('init', arguments);
		}
		,afterBuild:function(){
			var self = this;
			this.append($([
				'<div class="" pub-name="grid" pub-mod="blackMagic/grid/run.base"/>'
			].join('')));


			this.buildFromTemplate({
				grid: {
					'hasSearch': true,
					'hasFilter': false
				}
			});

			self.createAsync('rightPop','blackMagic/popwin/run.right');
		}
		,onInputClick: function(ev) {
			var url = '';
			switch (ev.name) {
				case 'generate':
					return window.open('api/admin/ptcGenerate');
					break;
				case 'importFile':
					url = 'ptc/import';
					break;
				case 'add':
					var mod = this.$.editPop;
					mod && mod.reset().show();
					return;
				default :
					return false;
			}

			pubjs.controller.navigate(url);
		}
		,reload: function() {
			this.$.grid.load();
		}
		,reset: function() {

		}
		,onSaveRecord: function() {
			var self = this;
			var mod = self.$.editPop;
			mod && mod.hide();
			self.$.grid.reload();
		}
		,onListOpClick:function(ev){
			//编辑单条记录
			var self = this;
			var c = self.getConfig();
			var id = ev.param.data._id;
			if(ev.param.op == 'edit')
			{
				var mod = self.$.editPop;
				mod && mod.reset().reload(id).show();
			}
			else if(ev.param.op == 'kick')
			{
				pubjs.confirm(LANG('确认要踢此用户吗'), function(res) {
					if(res) {
						pubjs.loading.show();
						pubjs.mc.send(c.urlRemove, {_id: id}, function(err) {
							pubjs.loading.hide();
							if(err) {
								pubjs.alert(err.message);
								return false;
							}

							pubjs.alert(LANG('删除成功'));
							self.reload();
						});
					}
				});
			}
			else if(ev.param.op == 'right')
			{

				if(self.$.rightPop)
				{
					self.$.rightPop.reset()
						.show()
						.setData(ev.param.data);
				}
			}
			return false;
		}
	});

	exports.list = List;
});